package main.objects;

import main.FactageSmet;
import main.MainFrame;
import main.MsSQLConnect;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SmetResource {

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getUnits() {
        return units;
    }

    private int id;

    public int getTitulId() {
        return titulId;
    }

    private int titulId;
    private String name;

    public String getCode() {
        return code;
    }

    private String code;
    private String type;
    private String units;

    public SmetResource(String name, String code, String type, String units, int titulId) {

        this.name = name;
        this.code = code;
        this.type = type;
        this.units = units;
        this.titulId = titulId;

    }

    public SmetResource(ResultSet rs) throws SQLException {

        this.id = rs.getInt("id");
        this.code = rs.getString("code");
        this.titulId = rs.getInt("titul_id");
        this.name = rs.getString("name");
        this.name = rs.getString("type");
        this.name = rs.getString("units");

    }

    public SmetResource() {

    }

    public void create() {
        try {

            FactageSmet.IMPORT_FACTAGE_CONNECTION = MsSQLConnect.ConnectDB("ImportFactage");

            Statement stat = FactageSmet.IMPORT_FACTAGE_CONNECTION.createStatement();

            stat.executeUpdate(
                    "INSERT INTO SmetResources VALUES (" +
                            titulId + ", '"
                            + code + "', '"
                            + name + "', '"
                            + type + "', '"
                            + units + "')  select @@identity as ident", Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = stat.getGeneratedKeys();

            if (rs.next()){
                id = rs.getInt(1);
            }

            rs.close();
            stat.close();
            FactageSmet.IMPORT_FACTAGE_CONNECTION.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        MainFrame.CURRENT_TITUL.getTitulResources().put(code, this);

    }

}
